﻿using System;
using System.Runtime.Serialization;
using XcoAppSpaces.Contracts.Communication;

namespace XcoAppSpaces.Serialization.Binary
{
	class XcoCommErrorSerializationSurrogate : ISerializationSurrogate
	{
		public void GetObjectData(object obj, SerializationInfo info, StreamingContext context)
		{
			var msg = (XcoCommunicationError)obj;
			info.AddValue("Error", msg.Error);
			info.AddValue("Info", msg.Info);
			info.AddValue("SourceAddress", msg.SourceAddress);
		}

		public object SetObjectData(object obj, SerializationInfo info, StreamingContext context, ISurrogateSelector selector)
		{
			obj = new XcoCommunicationError()
				{
					Error = (Exception)info.GetValue("Error", typeof(Exception)),
					Info = info.GetString("Info"),
					SourceAddress = info.GetString("SourceAddress"),
				};
			return obj;
		}
	}
}